<?php ob_start(); session_start();
include("../../Connections/connect_admin.php");
if($_POST["task"]=="add")
{
	$topic=intval($_POST['topic']);
	$type=intval($_POST['type']);
	$title=$_POST['title'];
	$desc=$_POST['description'];
	$content=$_POST['content'];
	$user=$_SESSION['username'];
	$review=0;
	if($_POST['review']=='reviewed')
	{
		$review=1;
	}
	$ishot=0;
	if($_POST['ishot']=='ishot')
	{
		$ishot=1;
	}
	
	$target_path = "../../Images/Img_News/";
	
	if(isset($_FILES['updfile'])) 
	{
	$target_path = $target_path.basename( $_FILES['updfile']['name']); 
	if ( !preg_match('/\.(jpg|gif|png)$/i',basename($_FILES['updfile']['name'] )) )
	{ header('location:../WriteNews.php?e=0');}
	if($_FILES['updfile']['size']>1024*1024)
	{header('location:../WriteNews.php?e=2');}
	if (file_exists($target_path))
		  {
		  	unlink($target_path);
		  }
	if(move_uploaded_file($_FILES['updfile']['tmp_name'], $target_path)) 
	{
		$image=basename( $_FILES['updfile']['name']);
		
	} else{
		header('location:../WriteNews.php?e=1');
	}
	}
	$query="INSERT INTO news(TopicID,NewsType,Title,Description,Image,Content,IsHot,IsReview,CreatedUser) VALUES($topic,$type,'$title','$desc','$image','$content',$ishot,$review,'$user')";
	if(mysql_query($query))
	{
		header('location:../News.php?e=0');
	}
	else
	{
		header('location:../News.php?e=1');
	}

}
if($_POST["task"]=="delete")
{
	$user=$_POST["user"];
	$newsID=intval($_POST["newsid"]);
	$query="UPDATE `news` SET IsDeleted = 1,DeletedDate = NOW(),DeletedUser = '$user' WHERE NewsID =".$newsID;
	$result=mysql_query($query);
		 $response = array(
        'ok' => true, 
        'msg' => "Xóa tin / bài viết thành công");		
	
	echo json_encode($response);
}
else if($_POST["task"]=="update")
{
	$newsid=intval($_POST['newsid']);
	$topic=intval($_POST['topic']);
	$type=intval($_POST['type']);
	$title=$_POST['title'];
	$desc=$_POST['description'];
	$content=$_POST['content'];
	$user=$_SESSION['username'];
	$review=0;
	$image=$_POST['image'];
	if($_POST['review']=='reviewed')
	{
		$review=1;
	}
	$ishot=0;
	if($_POST['ishot']=='ishot')
	{
		$ishot=1;
	}
	
	$target_path = "../../Images/Img_News/";
	
	if($_FILES['updfile']['tmp_name']!='') 
	{
	$target_path = $target_path.basename( $_FILES['updfile']['name']); 
	if ( !preg_match('/\.(jpg|gif|png)$/i',basename($_FILES['updfile']['name'] )) )
	{ header('location:../WriteNews.php?e=0');}
	if($_FILES['updfile']['size']>1024*1024)
	{header('location:../WriteNews.php?e=2');}
	if (file_exists($target_path))
		  {
		  	unlink($target_path);
		  }
	if(move_uploaded_file($_FILES['updfile']['tmp_name'], $target_path)) 
	{
		$image=basename( $_FILES['updfile']['name']);
		
	} else{
		header('location:../WriteNews.php?e=1');
	}
	}
	$query="UPDATE news SET TopicID = $topic,NewsType = $type,Title = '$title',Description = '$desc',Image ='$image' ,Content = '$content',IsHot = $ishot,IsReview = $review,UpdatedDate = NOW(),UpdatedUser = '$user' WHERE NewsID=".$newsid;
	if(mysql_query($query))
	{
		header('location:../News.php?e=2');
	}
	else
	{
		header('location:../News.php?e=3');
	}

}
?>